package com.zxl.yjb.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.FileUploadBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RealNameAuthenticationActivity extends BaseActivity {


    @BindView(R.id.tv_page_title)
    TextView tvPageTitle;
    @BindView(R.id.tv_userName)
    EditText tvUserName;
    @BindView(R.id.tv_idCardNum)
    EditText tvIdCardNum;
    @BindView(R.id.iv_idCard_face)
    ImageView ivIdCardFace;
    @BindView(R.id.iv_idCard_noface)
    ImageView ivIdCardNoface;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;

    private int CHOOSE_FACE_REQUEST = 10001;
    private int CHOOSE_NOFACE_REQUEST = 10002;
    private String faceIdCardUrl;
    private String nofaceIdCardUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_name_authentication);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void initView() {

        tvPageTitle.setText("实名认证");
    }


    @OnClick({R.id.iv_back, R.id.iv_idCard_face, R.id.iv_idCard_noface, R.id.tv_cancel1, R.id.tv_verification})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_idCard_face:
                DisplayUtil.openAlbum(RealNameAuthenticationActivity.this, 1, null, CHOOSE_FACE_REQUEST);

                break;
            case R.id.iv_idCard_noface:
                DisplayUtil.openAlbum(RealNameAuthenticationActivity.this, 1, null, CHOOSE_NOFACE_REQUEST);

                break;
            case R.id.tv_cancel1:
                finish();
                break;
            case R.id.tv_verification:
                submit();
                break;
        }
    }

    private void submit() {
        String realName = tvUserName.getText().toString();
        String idCard = tvIdCardNum.getText().toString();

        if (TextUtils.isEmpty(realName)) {
            ToastUtils.show(getApplicationContext(), "请填写真实姓名");
            return;
        }
        if (TextUtils.isEmpty(idCard)) {
            ToastUtils.show(getApplicationContext(), "请填写身份证号");
            return;
        }

        if (TextUtils.isEmpty(faceIdCardUrl)) {
            ToastUtils.show(getApplicationContext(), "请填上传身份证正面照");
            return;
        }
        if (TextUtils.isEmpty(nofaceIdCardUrl)) {
            ToastUtils.show(getApplicationContext(), "请填上传身份证反面照");
            return;
        }
        DataManager dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("name", realName);
        map.put("idnum", idCard);
        map.put("img_face", faceIdCardUrl);
        map.put("img_noface", nofaceIdCardUrl);
         dataManager.addIdCard(new ProgressObserver<Object>(new ObserverOnNextListener() {
             @Override
             public void onNext(Object o) {
//                 ToastUtils.show(getApplicationContext(),o.toString());
                 startActivity(new Intent(RealNameAuthenticationActivity.this,MyRealInfoActivity.class));
                 finish();
             }
         }, this), map);

    }

    /**
     * 图片上传
     *
     * @param imgPath
     */

    private void uploadPic(List<LocalMedia> imgPath,ObserverOnNextListener listener) {
        DataManager dataManager = new DataManager();

        dataManager.uploadMultiPicture(new ProgressObserver<FileUploadBean>(listener, this), imgPath);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_FACE_REQUEST) {// 图片选择结果回调
                List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                if (localMedia != null && localMedia.size() > 0) {
                    LocalMedia localMediaPath = localMedia.get(0);
                    String picFacePath = localMediaPath.getPath();
                    GlideApp.with(getApplication()).load(picFacePath).into(ivIdCardFace);
                    uploadPic(localMedia,  new ObserverOnNextListener<FileUploadBean>() {
                        @Override
                        public void onNext(FileUploadBean fileUploadBean) {
                            faceIdCardUrl = fileUploadBean.getUrls().get(0);

                        }
                    });
                }
            } else if (requestCode == CHOOSE_NOFACE_REQUEST) {
                List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                if (localMedia != null && localMedia.size() > 0) {
                    LocalMedia localMediaPath = localMedia.get(0);
                    String picNoFacePath = localMediaPath.getPath();
                    GlideApp.with(getApplication()).load(picNoFacePath).into(ivIdCardNoface);
                    uploadPic(localMedia,  new ObserverOnNextListener<FileUploadBean>() {
                        @Override
                        public void onNext(FileUploadBean fileUploadBean) {
                            nofaceIdCardUrl = fileUploadBean.getUrls().get(0);

                        }
                    });
                }

            }
        }
    }
}
